<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/10
 * Time: 13:39
 */

namespace app\index\model\user;


use app\index\model\area\Area;
use basic\ModelBasic;
use think\Db;
use traits\ModelTrait;
use think\Session;

class User extends ModelBasic
{
    use ModelTrait;
    public static function tableName()
    {
        return 'user';
    }

    /**
     * 获得当前登陆用户UID
     * @return int $uid
     */
    public static function getActiveUid()
    {
        $uid = null;
        if(Session::has('loginUid','wap')) $uid = Session::get('loginUid','wap');
        if(!$uid && Session::has('loginOpenid','wap') && ($openid = Session::get('loginOpenid','wap')))
            $uid = WechatUser::openidToUid($openid);
        if(!$uid) exit(exception('请登陆!'));
        return $uid;
    }


    public static function getUserInfo($uid)
    {
        $userInfo = self::where('uid',$uid)->find();
        if(!$userInfo) exception('读取用户信息失败!');
        return $userInfo->toArray();
    }

    public static function GetRegUser($account='',$password=''){
        $pwd = md5($password);
        //$user=Db::name(self::tableName())->where('pwd',$pwd)->where('account','like',"%{$account}%")->find();
        $user=Db::name(self::tableName())->where('pwd',$pwd)->where('user_name',$account)->find();
        if(!empty($user)){
            return $user['uid'];
        }
        return 0;
    }

    public static function AddUser($data){
        $ip = $_SERVER["REMOTE_ADDR"];
        $AddUser = [
            'user_name' =>$data['user_name'],
            'nick_name' =>$data['user_name'],
            'pwd' =>md5($data['one_password']),
            'add_time' =>time(),
            'last_time' =>time(),
            'last_ip' =>!empty($ip)? $ip : 0,
        ];
        if(self::set($AddUser)===false){
            return false;
        };
        return true;
    }
}